{% extends 'base.html' %}

{% block title %}添加使用记录 - 维修管理系统{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title mb-0">添加使用记录</h4>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">备件</label>
                            <select name="spare_part" class="form-select" required>
                                <option value="">-- 选择备件 --</option>
                                {% for part in spare_parts %}
                                <option value="{{ part.id }}" 
                                    data-stock="{{ part.current_stock }}"
                                    data-unit="{{ part.unit }}">
                                    {{ part.name }} ({{ part.model }}) - 库存: {{ part.current_stock }} {{ part.unit }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">使用数量</label>
                            <input type="number" name="quantity" class="form-control" 
                                   min="1" step="1" required 
                                   placeholder="请输入使用数量">
                            <div class="form-text">当前库存: <span id="current-stock">0</span> <span id="stock-unit">个</span></div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">使用人</label>
                            <select name="used_by" class="form-select" required>
                                <option value="">-- 选择使用人 --</option>
                                {% for user in users %}
                                <option value="{{ user.id }}">{{ user.username }} ({{ user.get_full_name|default:user.username }})</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">关联工单 (可选)</label>
                            <select name="ticket" class="form-select">
                                <option value="">-- 选择工单 --</option>
                                {% for ticket in tickets %}
                                <option value="{{ ticket.id }}">{{ ticket.title }} ({{ ticket.ticket_number }})</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">用途说明</label>
                        <textarea name="purpose" class="form-control" rows="3" 
                                  placeholder="请输入使用用途说明..." required></textarea>
                    </div>

                    <div class="d-flex justify-content-between">
                        <a href="{% url 'spare_parts:usage_list' %}" class="btn btn-secondary">
                            <i class="bi bi-arrow-left"></i> 返回列表
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-circle"></i> 提交使用记录
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const sparePartSelect = document.querySelector('select[name="spare_part"]');
    const quantityInput = document.querySelector('input[name="quantity"]');
    const currentStockSpan = document.getElementById('current-stock');
    const stockUnitSpan = document.getElementById('stock-unit');

    sparePartSelect.addEventListener('change', function() {
        const selectedOption = this.options[this.selectedIndex];
        if (selectedOption.value) {
            const stock = selectedOption.getAttribute('data-stock');
            const unit = selectedOption.getAttribute('data-unit');
            currentStockSpan.textContent = stock;
            stockUnitSpan.textContent = unit;
            
            // 设置最大数量为当前库存
            quantityInput.setAttribute('max', stock);
        } else {
            currentStockSpan.textContent = '0';
            stockUnitSpan.textContent = '个';
            quantityInput.removeAttribute('max');
        }
    });

    // 表单验证
    document.querySelector('form').addEventListener('submit', function(e) {
        const selectedPart = sparePartSelect.value;
        const quantity = parseInt(quantityInput.value);
        const stock = parseInt(currentStockSpan.textContent);
        
        if (selectedPart && quantity > stock) {
            e.preventDefault();
            alert('使用数量不能超过当前库存！');
        }
    });
});
</script>
{% endblock %}
